package br.com.ldml2;

import br.com.ldml.Tripla;
import br.com.util.Escritor;
import br.com.util.TripleSet;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("merge")
public class Merge implements LdmlCommand {
	@XStreamAsAttribute
	private String dataset1, dataset2, output;
	
	public String getDataset1() {
		return dataset1;
	}

	public void setDataset1(String dataset1) {
		this.dataset1 = dataset1;
	}

	public String getDataset2() {
		return dataset2;
	}

	public void setDataset2(String dataset2) {
		this.dataset2 = dataset2;
	}

	@Override
	public void exec(String tmpFileName) {
		TripleSet tsIn1 = new TripleSet(dataset1);
		TripleSet tsIn2 = new TripleSet(dataset2);
		
		TripleSet tsOut = new TripleSet(dataset2);
		
		for(String subject : tsIn1.getTripleSet().keySet()) {
			for (String property : tsIn1.getTripleSet().get(subject).keySet()) {
				for(String object : tsIn1.getTripleSet().get(subject).get(property)) {
					Tripla t = new Tripla(subject, property, object);
					
					tsOut.addTriple(t);
				}
			}
		}
		
		for(String subject : tsIn2.getTripleSet().keySet()) {
			for (String property : tsIn2.getTripleSet().get(subject).keySet()) {
				for(String object : tsIn2.getTripleSet().get(subject).get(property)) {
					Tripla t = new Tripla(subject, property, object);
					
					tsOut.addTriple(t);
				}
			}
		}
		
		XStream xs2 = new XStream();
		Escritor e = new Escritor();
		e.abreArquivo(tmpFileName, true);
		e.gravaArquivo(new String[] { xs2.toXML(tsOut) } );
		
		e.fechaArquivo();
	}

	@Override
	public String getOutput() {
		return output;
	}

	@Override
	public void setOutput(String output) {
		this.output = output;
	}
}
